import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import Tagger.AutoTagAlgorithm;

public class FileSizeAutoTagAlgorithm implements AutoTagAlgorithm {
	private int smallMaxSize;
	private int mediumMaxSize;
	private int bigMaxSize;
	
	//constructor.
	public FileSizeAutoTagAlgorithm(){
		//put default values.
		smallMaxSize=5000;
		mediumMaxSize=500000;
		bigMaxSize=5000000;
		
		//search for "FileSizeAuotTagAlgorithm.txt" file ,to get users definitions for small,medium and big.
		
		Path file = Paths.get("FileSizeAuotTagAlgorithm.txt"); //here the text file should be.
		
		//case the file "FileSizeAuotTagAlgorithm.txt" exists.
		if(file.exists()) 
		{
			String words[]=new String[3];
			int sizes[]=new int[3];
			boolean rightSyntax=true;
			InputStream in = null;
			try {
			    in = file.newInputStream();
			    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			    String line = null;
			    //get a line, check if it's syntax is good. case good put it in the array.
			    int i;
			    for (i=0; i<3&&(line = reader.readLine()) != null;i++) {			    	
			    	String[] data=line.split(" ");
			    	if(data.length!=2){//case not enough values in row.
			    		System.out.println("worng syntax on FileSizeAuotTagAlgorithm.txt file !\t - default values used");
			    		rightSyntax=false;
			    		break;
			    	}
			    	words[i]=data[0];
			    	sizes[i]=Integer.parseInt(data[1]);
			    }
			    if(i<3||!words[0].equals("Small")||!words[1].equals("Medium")||!words[2].equals("Big")){//case  not enough rows, or wrong words. 
			    	System.out.println("worng syntax on FileSizeAuotTagAlgorithm.txt file !\t - default values used");
			    	rightSyntax=false;
			    }
			    if(sizes[1]<sizes[0]||sizes[2]<sizes[0]||sizes[2]<sizes[1]){//case sizes given by user are not at increasing order.
			    	System.out.println("sizes miss-match on FileSizeAuotTagAlgorithm.txt file !\t - default values used");
			    	rightSyntax=false;
			    }
			    if(rightSyntax){//case FileSizeAuotTagAlgorithm.txt file is well builded, then use it.
			    	smallMaxSize=sizes[0];
			    	mediumMaxSize=sizes[1];
			    	bigMaxSize=sizes[2];
			    	
			    }
			} 
			catch (IOException x) {
			}
			 //close the file
			finally {
			    if (in != null)
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		}
		else //case the file not exist. create it with default values.
		{
			BufferedWriter out=null;
			try {
				out = new BufferedWriter(new FileWriter(file.toString())); 
				String[] data={"Small 5000","Medium 500000","Big 5000000"};
				for(int i=0 ; i<3 ; i++){
					out.write(data[i]);
					out.newLine();
				}
			} 
			catch (IOException x) {
			}
			 //close the file
			finally {
			    if (out != null)
					try {
						out.flush();
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		}
		

	}
	public String getInfo() {
		return new String(
				"This Algorithm return tags:\n"+
				"small, medium, big and huge,\n"+
				"according to file's size. \n"+
				"The user can also change the values\n"+
				"for that tags. In order to do that,\n"+
				"you have to edit the file :\n"+
				"'FileSizeAuotTagAlgorithm.txt'.\n"+
				"The sytanx have to be \n: 'Small %d\nMedium %d\nBig %d'.\n"+
				"This algorithm returns the first tag(searching by order :\n"+
				"Small,Medium,Big)that it's size \n, is bigger then the file's size.)"
				);
	}

	public String[] getTagsForFile(File file) {
		    
		    if (!file.exists() || !file.isFile()) {
		      System.out.println("File doesn\'t exist");
		      return null;
		    }
		    
		    //Here we get the actual size
		    long fileSize= file.length();
		    if(fileSize<=smallMaxSize){
		    	return new String []{"Small"};
		    }
		    else if(fileSize<=mediumMaxSize){
		    	return new String []{"Medium"};
		    }
		    else if(fileSize<=bigMaxSize){
		    	return new String []{"Big"};
		    }
		    else {
		    	String[] tags={"Huge"};
		    	return tags;
		    }
	}

}
